<template>
  <div>
    <section class="header clearfix">
      <span>缴纳平台管理</span>
      <span class="iconfont iconadd1" @click="add"> <i>添加</i></span>
      <span class="iconfont iconquyuguanli" @click="bind">
        <i>绑定区域</i></span
      >
    </section>
    <div class="search-box" @keyup.enter="searchFun">
      <i class="iconfont iconsearch1"></i>
      <el-input
        placeholder="输入平台名称进行搜索"
        class="search"
        v-model.trim="PName"
        clearable
      ></el-input>
    </div>
    <div class="search-box search-box-add">
      <section class="option">
        <span>区域:</span>
        <el-select
          v-model="areaCode"
          filterable
          clearable
          @change="searchFun"
          placeholder="请选择"
        >
          <el-option
            v-for="item in areaCodeOptions"
            :key="item.areaCode"
            :label="item.areaName"
            :value="item.areaCode"
          >
          </el-option>
        </el-select>
      </section>
    </div>
    <div class="table">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        v-loading="loading"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="platformId" label="ID"></el-table-column>
        <el-table-column prop="platformName" label="名称"></el-table-column>
        <el-table-column
          prop="isDefault"
          label="是否默认"
          :formatter="isDefaultFormatter"
        ></el-table-column>

        <el-table-column label="详情" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="remove(scope.row.PId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="current"
        :page-sizes="[6, 12, 24, 36]"
        :page-size="pageSize"
        layout="sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>
    <!--新增对话框-->
    <el-dialog
      title="新增缴纳平台"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :show-close="false"
      width="600px"
      center
    >
      <el-form :model="form">
        <el-form-item label="平台名称" :label-width="formLabelWidth">
          <el-input
            v-model="form.PName"
            clearable
            placeholder="请输入平台名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogFormVisible = false;
            addLoading = false;
          "
          >取 消</el-button
        >
        <el-button type="primary" :loading="addLoading" @click="confirmAdd()"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!--修改对话框-->
    <el-dialog
      title="编辑缴纳平台"
      :visible.sync="dialogEdit"
      :close-on-click-modal="false"
      :show-close="false"
      center
      width="600px"
    >
      <el-form :model="form">
        <el-form-item label="平台名称" :label-width="formLabelWidth">
          <el-input
            v-model="form.PName"
            clearable
            placeholder="请输入平台名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否默认" :label-width="formLabelWidth">
          <el-select v-model="form.isDefault" placeholder="请选择">
            <el-option
              v-for="item in isDefaultOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogEdit = false;
            editLoading = false;
          "
          >取 消</el-button
        >
        <el-button
          type="primary"
          :loading="editLoading"
          @click="confirmUpdate()"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!--区域对话框-->
    <el-dialog
      title="绑定区域"
      :visible.sync="dialogBind"
      :close-on-click-modal="false"
      :show-close="false"
      center
      width="600px"
    >
      <el-form>
        <el-form-item label="区域" :label-width="formLabelWidth">
          <el-select v-model="bindAreaCode" filterable placeholder="请选择">
            <el-option
              v-for="item in areaCodeOptions"
              :key="item.areaCode"
              :label="item.areaName"
              :value="item.areaCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogBind = false;
            bindLoading = false;
          "
          >取 消</el-button
        >
        <el-button type="primary" :loading="bindLoading" @click="bindArea()"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template> 

<script>
import base from "@/assets/js/base";
import { requestApiPost } from "../api/api";
import { reqRep } from "../report/reqReport";
import { resRep } from "../report/resReport";
export default {
  name: "platformManagement",
  data() {
    return {
      loading: false,
      addLoading: false,
      editLoading: false,
      bindLoading: false,
      areaCode: "",
      bindAreaCode: "",
      formLabelWidth: "160px",
      isNumber: 0,
      PName: "",
      tableData: [],
      current: 1,
      pageSize: 6,
      total: null,
      form: {},
      dialogFormVisible: false,
      dialogEdit: false,
      dialogBind: false,
      paymentOptions: [],
      areaCodeOptions: [],
      isDefaultOptions: [
        {
          value: 1,
          label: "默认",
        },
        {
          value: 2,
          label: "非默认",
        },
      ],
    };
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getData();
    },
    handleCurrentChange(e) {
      this.current = e;
      this.getData();
    },
    typeChange(e) {
      if (e == 0) {
        this.$set(this.form, "companyScale", "");
        this.$set(this.form, "personScale", "");
      } else {
        this.$set(this.form, "defaultVal", "");
      }
    },
    //编辑
    edit(info) {
      this.$set(this.form, "PId", info.platformId);
      this.$set(this.form, "PName", info.platformName);
      this.$set(this.form, "isDefault", info.isDefault);

      this.dialogEdit = true;
    },
    //确认更新
    confirmUpdate() {
      if (!this.form.PName) {
        this.$message.warning("请输入平台名称");
        return;
      }
      let data = {
        PId: this.form.PId,
        PName: this.form.PName,
        isDefault: this.form.isDefault,
      };
      this.editLoading = true;
      let reqReport = reqRep({ traCode: "_EDIT_PAY_PLATFORM", data: data });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
          this.editLoading = false;
        } else {
          this.$message({
            message: "成功",
            type: "success",
          });
          this.dialogEdit = false;
          this.editLoading = false;
          this.getData();
        }
      });
    },
    //删除
    remove(id) {
      this.$confirm("确定删除该缴纳平台?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.removePayment(id);
        })
        .catch(() => {});
    },
    removePayment(id) {
      var reqData = {
        PId: id,
      };
      let reqReport = reqRep({ traCode: "_DEL_PAY_PLATFORM", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getData();
        }
      });
    },
    //新增
    add() {
      this.$set(this.form, "PName", "");
      this.dialogFormVisible = true;
    },
    //确认新增
    confirmAdd() {
      if (!this.form.PName) {
        this.$message.warning("请输入平台名称");
        return;
      }
      let data = {
        PName: this.form.PName,
      };
      this.addLoading = true;
      let reqReport = reqRep({ traCode: "_ADD_PAY_PLATFORM", data: data });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
          this.addLoading = false;
        } else {
          this.$message({
            message: "成功",
            type: "success",
          });
          this.dialogFormVisible = false;
          this.addLoading = false;
          this.getData();
        }
      });
    },
    handleSelectionChange(val) {
      this.checkedArray = val;
    },
    //绑定
    bind() {
      if (this.checkedArray.length <= 0) {
        this.$message.warning("请先选择缴纳平台");
        return;
      }
      this.checkedId = this.checkedArray.map((e) => {
        return e.platformId;
      });
      this.bindAreaCode = "";
      this.dialogBind = true;
    },
    //绑定区域
    bindArea() {
      if (!this.bindAreaCode) {
        this.$message.warning("请选择区域");
        return;
      }
      let PIds = this.checkedId.join();

      let data = {
        PIds: PIds,
        areaCode: this.bindAreaCode,
      };
      this.bindLoading = true;
      let reqReport = reqRep({
        traCode: "_BIND_AREA_PAY_PLATFORM",
        data: data,
      });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
          this.bindLoading = false;
        } else {
          this.$message({
            message: "成功",
            type: "success",
          });
          this.dialogBind = false;
          this.bindLoading = false;
          this.getData();
        }
      });
    },
    //获取区域列表
    getAreaCode() {
      var reqData = {
        grade: 1,
        isStart: 0,
        current: 1,
        pageSize: 999,
      };
      let reqReport = reqRep({ traCode: "_LIST_AREA", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.areaCodeOptions = resReport.data.records;
        }
      });
    },
    searchFun() {
      this.current = 1;
      this.tableData = [];
      this.getData();
    },
    //获取列表
    getData() {
      this.loading = true;
      var reqData = {
        PName: this.PName,
        areaCode: this.areaCode,
        current: this.current,
        pageSize: this.pageSize,
      };
      let reqReport = reqRep({ traCode: "_LIST_PAY_PLATFORM", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        this.loading = false;
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.tableData = resReport.data.records;
          this.total = parseInt(resReport.data.total);
        }
      });
    },
    areaCodeFilter(a, b, c) {
      for (let index = 0; index < this.areaCodeOptions.length; index++) {
        if (this.areaCodeOptions[index].areaCode == c) {
          return this.areaCodeOptions[index].areaName;
        }
      }
    },
    typeFilter(a, b, c) {
      if (c == 1) {
        return "比例";
      } else {
        return "值";
      }
    },
    defaultValFormatter(a, b, c) {
      if (c >= 0) {
        return c;
      } else {
        return "-";
      }
    },
    valueFormatter(a, b, c) {
      if (c > 0) {
        return c + "%";
      } else {
        return "-";
      }
    },
    isDefaultFormatter(a, b, c) {
      if (c == 1) {
        return "默认";
      } else {
        return "非默认";
      }
    },
  },
  created() {
    this.checkedArray = [];
    this.getData();
    this.getAreaCode();
  },
};
</script>
<style scoped>
.header {
  padding: 15px;
  height: 50px;
  box-sizing: border-box;
  font-size: 14px;
}
.header .org {
  float: right;
  cursor: pointer;
}
.org:hover {
  color: #13cb6e;
}
.opt-user {
  padding: 20px 15px;
}
.opt-user > section {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}
.opt-user i {
  font-size: 20px;
  margin-right: 5px;
  vertical-align: middle;
}
.header .iconfont {
  float: right;
  font-size: 14px;
  color: #13cb6e;
  cursor: pointer;
  margin-left: 15px;
}
.header i {
  margin-left: 4px;
  color: #13cb6e;
  font-size: 14px;
  font-style: normal;
}
.header .iconquyuguanli {
  font-size: 17px;
}
.search-box {
  height: 50px;
  line-height: 50px;
  border-top: 1px solid #eee;
  border-bottom: 1px solid #eee;
}
.search-box-add {
  border-top: none;
}
.search-box i {
  margin-left: 15px;
  vertical-align: middle;
}
.search-box .search {
  display: inline-block;
  width: calc(100% - 36px);
  box-sizing: border-box;
}
.search-box .option {
  display: inline-block;
  margin-left: 15px;
}
.search-box .el-select {
  width: 150px;
}
.search-box .el-select input {
  min-width: 100px;
  background-color: #eee;
}
.search-box .batchChange {
  display: inline-block;
  color: #999;
  font-size: 14px;
  cursor: pointer;
}
i {
  font-size: 16px;
  color: #999;
}
</style>
